/**
 * Created by acer on 2015/7/21.
 */
var upload='/file/upload'


var waittingTime=5000



var maxPage;
var currentPage;
var isChangePage=false;
var myStartKey
var searchAllCount=0
var currentId
var findEachData
$(document).ready(function(){
    getDealedList();
    getUndealList()
    $('#modal-listDetailListModal').find('input,select,textarea').attr('disabled',true)
    var ulimitedRequire =setInterval(function(){
        getDealedList();
        getUndealList()
    },waittingTime)
})

var Mymodel={}
Mymodel.GetDealingFileList=ko.observableArray([]);
Mymodel.GetPaging=ko.observableArray([]);
Mymodel.GetUndealingFileList=ko.observableArray([]);
Mymodel.GetErrorFileList=ko.observableArray([]);


function getDealedList(){
    var params = {
        url:appSocket+'/file/findFileListByStatus?status=1',
        data:{},
        method:'get'
    }
    ajaxRequest(params,function(data){
        Mymodel.GetDealingFileList(data.recordset)
        if(data.recordset.length!=''){
            for(var i in data.recordset){
                data.recordset[i].FERRORMESSAGE=JSON.stringify(data.recordset[i].FERRORMESSAGE)
            }
            Mymodel.GetErrorFileList(data.recordset)
            $('#errorMessage').show()
        }
        else{
            $('#errorMessage').hide()
            $('#dropZoneOwn').show()
            $('#startTranslateIt').show()
        }
    })
}

function getUndealList(){
    var params = {
        url:appSocket+'/file/findFileListByStatus?status=0',
        data:{},
        method:'get'
    }
    ajaxRequest(params,function(data){
        var contentFile=data.recordset
        Mymodel.GetUndealingFileList(contentFile);
        //Mymodel.GetErrorFileList(contentFile);
    })
}

function clean(){
    //$('#modal-addBackEnd').modal({backdrop: 'static',keyboard:false});
    $('#modal-listDetailListModal').find('input:not(input:radio)').val('')
    $('#modal-listDetailListModal').find('input:radio').prop('checked',false)
}


function createInstruction(){
    if(alertEmpty('#modal-addBackEnd')){
        var status
        var enterpriseName=$('#enterpriseName').val()
        var account=$('#account').val()
        var password=$('#password').val()
        if(password.length<8){
            alert('密码长度应大于8位')
            return false;
        }
        var email=$('#email').val()
        var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if(reg.test(email)){
        }else{
            alert("请输入正确的邮箱格式");
            return false
        }
        $('input:radio[name="priority"]').each(function(){
            if($(this).is(':checked')==true){
                status=$(this).val()
            }
        })
        if(status==undefined){
            alert('请选择状态')
            return false
        }
        var data = {
            "enterpriseName":enterpriseName,
            "account": account,
            "password": password,
            "email": email,
            "status": status
        }
        sendCreateInstruction(data,sendCreateInstructionCallback)
    }
}

function sendCreateInstructionCallback(data){
    if(data.data.success==false){
        alert(data.data.message)
        return false
    }
    $('#modal-addBackEnd').modal('hide')
    toPage=1
    searchAllCount=0
    var data = {
        "startKey":"",
        "lastKey":'',
        "jumpPage":0,
        "limit":pageSize
    }
    getBidsList(data,getBidsListCallback)
}

function checkIt(k){
    currentId= k.getAttribute('selfId')
    window.open('orderListReceive.html?batchID='+currentId)
    //$('#modal-modifyBackEnd').find('input:not(input:radio)').val('')
    //$('#modal-modifyBackEnd').find('input:radio').prop('checked',false)
    ////var data={
    ////    _id: currentId
    ////}
    ////sendCheckedBid(data,sendCheckedBidCallback)
    //$('#modal-listDetailListModal').modal('show')
}

function sendCheckedBidCallback(data){
    for(var i in data.data[0]){
        $('#'+i+'Modify').val(data.data[0][i])
        if(i=='status'){
            if(data.data[0][i]==0){
                $('#0').prop('checked',true)
            }
            else{
                $('#1').prop('checked',true)
            }
        }
    }
}

function uploadFileStart(){
//    $(event.currentTarget).attr('disabled',true)

    $('#dropZoneOwn').hide()
    $('#errorMessage').show()
    var params={
        url:appSocket+'/order/beginParseExcel',
        data:{},
        method:'get'
    }
    ajaxRequest(params,function(data){
        if(data.status=='200'){
            var content = data.recordset
            for(var i in content){
                content['fileName']=$('#'+content[i].batchID).children('.dz-filename').children('span').text()
            }
        }
        getDealedList();
        getUndealList();
    })
}


ko.applyBindings(Mymodel);
